
window.color=cc.spriteFrame

cc.Class({
    extends: cc.Component,

    properties: {

        blackSpriteFrame:{//白棋的图片
            default:null,
            type:cc.SpriteFrame
        },
        whiteSpriteFrame:{//白棋的图片
            default:null,
            type:cc.SpriteFrame
        },
        anima:{
          default:null,
          type:cc.Animation
        },
        // step:Number
        
    },

    // LIFE-CYCLE CALLBACKS:

    // this.ind = -1;   //位置下标

    // this.state = 0;   //状态 0未落子， 1白棋，2黑棋

    onLoad () {

    },

    init(ind){
      this.ind = ind;
    },

    trans:function(){

        //落子的实现
          var test=this
          if (selfIndex == -1){
            cc.systemEvent.emit("showTip", "您未参与游戏");
            return;
          }
          if (!isPlaying){
            cc.systemEvent.emit("showTip", "游戏已结束");
            return;
          }
          if (window.remainTime <= 1){
            return;
          }
          if (window.selfUserId != window.playUserId || gamestate=='stop'){
            cc.systemEvent.emit("showTip", "还未到你下棋的时候");
            return;
          }

          if(gamestate=='white' && this.getComponent(cc.Sprite).spriteFrame==null) //判断是否有棋子
          {
            this.setAnimaState(1);
            setTimeout(() => {
              test.getComponent(cc.Sprite).spriteFrame=test.whiteSpriteFrame  //将透明棋子变成白字
              //gamestate='black'   //每次点击时对棋子状态进行改变
              cc.log(gamestate)
              color = test.whiteSpriteFrame
              chessbox.push(test)    //棋子下棋后入栈

              test.state = 1;
              cc.systemEvent.emit('sendMessage', 1005, [test.ind]);
            }, 5);
            gamestate='stop'
          }
        else if(gamestate=='black' && this.getComponent(cc.Sprite).spriteFrame==null)
          {
            this.setAnimaState(2);
            setTimeout(() => {
              test.getComponent(cc.Sprite).spriteFrame=test.blackSpriteFrame //将透明棋子变成黑字
              //gamestate='white'   
              cc.log(gamestate)
              color=test.blackSpriteFrame
              chessbox.push(test)   //棋子下棋后入栈

              test.state = 2;

              cc.systemEvent.emit('sendMessage', 1005, [test.ind]);
            },5);
            gamestate='stop'
          }
        else {

          cc.systemEvent.emit("showTip", "这里有棋子");
          //alert("这里有棋子")
        } 

       // this.getComponent(cc.Sprite).spriteFrame=this.blackSpriteFrame

       //将棋子放进数组
    },

    setAnimaState(state, node){
      //this.anima.play(state == 1?"hongcha":"luquan");
    },

    getState(){
      this.state = 0;
      if (this.getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame){
        this.state = 1
      }
      if (this.getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame){
        this.state = 2
      }
      return this.state;
    },

    setState(state){
      this.state = state;
      if (state == 0){
        this.getComponent(cc.Sprite).spriteFrame = null;
      }else{
        this.getComponent(cc.Sprite).spriteFrame= this.state == 1 ? this.whiteSpriteFrame : this.blackSpriteFrame;
      }

      this.node.children[2].active = false;
    },

    //展示下棋落子
    setScaleBig(){
      this.node.children[2].active = true;
    },


    start () {

    },

    // update (dt) {},
});
